package com.example.dbcms.controller.user;

import com.example.dbcms.annotation.Log;
import com.example.dbcms.domain.Parking;
import com.example.dbcms.domain.User;
import com.example.dbcms.service.ParkingService;
import com.example.dbcms.service.UserService;
import com.example.dbcms.utils.ResultVoUtil;
import com.example.dbcms.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
@Api("普通用户停车位信息路由")
public class UserParkingController {

    @Autowired
    private ParkingService parkingService;

    @Autowired
    private UserService userService;

    /**
     * 普通用户查看停车位信息
     * @return
     */
    @GetMapping("/parkingInfo")
    @ApiOperation("普通用户查看停车位信息")
    @Log(type="跳转停车场信息页面")
    public String getResidentPayInfo(Parking parking, ModelMap modelMap){
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        User user = userService.getUserByUserAccount(userAccount);
        parking.setBuildingNo(user.getBuildingNo());
        parking.setRoomNo(user.getRoomNo());
        PageInfo<Parking> parkingPageInfo = parkingService.selectAllParking(parking);
        modelMap.put("parkingInfoList", parkingPageInfo.getList());
        modelMap.put("parkingInfo", parking);
        modelMap.put("pageInfo", parkingPageInfo);
        return "user/parking/index";
    }

    /**
     * 普通用户修改停车位信息
     * @param parking 居民信息
     * @return
     */
    @ApiOperation("普通用户修改停车位信息")
    @PostMapping("/editParking")
    @ResponseBody
    @Log(type = "普通用户修改停车位信息")
    public ResultVo editParking(Parking parking){
        if(parkingService.updateParkingInfoByUser(parking)==1){
            return ResultVoUtil.SAVE_SUCCESS;
        }
        return ResultVoUtil.error("更新失败");
    }

    /**
     * 通过id查找停车位信息跳转到编辑页面
     * @param id
     * @param modelMap
     * @return
     */
    @ApiOperation("弹出编辑停车位页面")
    @GetMapping("/editParking/{id}")
    @Log(type="弹出编辑停车位页面")
    public String editResidentInfo(@PathVariable("id") int id, ModelMap modelMap){
        modelMap.put("parkingInfo",parkingService.getParkingInfo(id));
        return "user/parking/edit";
    }

    /**
     * 弹出购买停车位界面
     * @return
     */
    @ApiOperation("弹出购买停车位界面")
    @GetMapping("/parking/add")
    @Log(type="弹出购买停车位界面")
    public String addParking(){
        return "user/parking/add";
    }

    /**
     * 普通用户购买停车位
     * @return
     */
    @ApiOperation("弹出购买停车位界面")
    @PostMapping("/parking/add")
    @ResponseBody
    @Log(type="弹出购买停车位界面")
    public ResultVo addParkingToDb(Parking parking){
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        User user = userService.getUserByUserAccount(userAccount);
        parking.setBuildingNo(user.getBuildingNo());
        parking.setRoomNo(user.getRoomNo());
        int result = parkingService.addParking(parking);
        if(result == 1){
            return ResultVoUtil.success("购买成功");
        }
        return ResultVoUtil.error("购买失败");
    }
}
